<template>
	<view>
		<view class="wrap">
			<u-search placeholder="请输入关键字" v-model="query.keyword" @search="searchKey" @custom="searchKey"></u-search>
			<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="reachBottom"
				v-if="stockList.length !== 0">
				<view class="page-box">
					<view class="order" v-for="(item, index) in stockList" :key="item.id">
						<view class="item">

							<view class="content">
								<view class="title u-line-2">{{item.skuName}}</view>
								<view class="type">年份: {{ item.placedYear }}</view>
								<view class="type">名称: {{ item.itemName }}</view>
								<view class="type">单价: {{ item.itemPrice }}</view>
								<view class="flex">
									<view class="type" v-if="item.showNumberBox!=true">库存: {{ item.num }}</view>
									<up-number-box v-model="item.num" v-if="item.showNumberBox" @change="changeNumBox">
										<template #minus>
											<view class="minus">
												<up-icon name="minus" size="12"></up-icon>
											</view>
										</template>
										<template #input>
											<text style="width: 55px;text-align: center;" class="input">库存:
												{{item.num}}</text>
										</template>
										<template #plus>
											<view class="plus">
												<up-icon name="plus" color="#FFFFFF" size="12"></up-icon>
											</view>
										</template>
									</up-number-box>
								</view>

								<view class="type">合计: {{ item.totalPrice }}</view>
							</view>
							<view class="right">
								<image :src="item.itemImg" mode="aspectFill"></image>
							</view>
						</view>
						<view class="bottom">

							<view class="evaluate btn" @click="getDetail(item.skuName)">查看明细</view>
							<view class="logistics btn" type="primary reverse" @click="getImage(item.skuName)">上传主图
							</view>
							<view class="logistics btn" @click="updateStock(item)">
								{{item.buttonName?item.buttonName:"库存更新"}}
							</view>
							<view class="logistics btn" @click="showModal(item.skuName)">编码更新</view>
						</view>
					</view>
					<u-loadmore :status="loadStatus[0]" bgColor="#f2f2f2"></u-loadmore>
				</view>
			</scroll-view>
			<scroll-view scroll-y style="height: 100%;width: 100%;" v-else>
				<view class="page-box">
					<view>
						<view class="centre">
							<image src="https://cdn.uviewui.com/uview/template/taobao-order.png" mode="">
							</image>
							<view class="explain">
								您还没有相关的库存
								<view class="tips">请先设置库存</view>
							</view>
							<view class="btn">随便逛逛</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>

	<view class="">
		<u-modal :show="show" title="编码更新" content="内容" @confirm="confirm" @cancel="show=false" :showCancelButton="true"
			ref="uModal" :asyncClose="true">
			<up-input v-model="upSkuCode" placeholder="请输入编码"></up-input>
		</u-modal>
	</view>
	<view class="">
		<u-modal :show="stockDetailShow" title="库存明细" content="库存明细" confirmText="跳转销售列表" @confirm="gotoSales('')"
			@cancel="stockDetailShow=false" :showCancelButton="true" ref="uModal1" >
			<view>
				<up-row customStyle="margin-bottom: 10px">
					<up-col span="auto">
						<up-text text="商品编码:"></up-text>
					</up-col>
					<up-col span="auto">
						<up-text :text="stockDetail.skuCode"></up-text>
					</up-col>
				</up-row>
				<up-row customStyle="margin-bottom: 10px">
					<up-col span="auto">
						<up-text text="商品名称:"></up-text>
					</up-col>
					<up-col span="auto">
						<up-text :text="stockDetail.itemName"></up-text>
					</up-col>
				</up-row>
				<up-list height="auto">
					<up-list-item v-for="(item, index) in stockDetail.itemSizes" :key="index">
						<text class="u-demo-block__title">尺码:{{item.itemSize}} 总库存:{{item.num}} 可用库存:{{item.availableStock}}</text>
						<view class="u-demo-block__content">
							<up-line-progress :percentage="(item.availableStock/item.num*100).toFixed(2)"></up-line-progress>
						</view>
						<up-line margin="5px"></up-line>
					</up-list-item>
				</up-list>
			</view>
		</u-modal>
	</view>
</template>



<script>
	import {
		stockList,
		stockListDetail,
		updateItemImg,
		uploadImg,
		addStock,
		updateSkuCode
	} from "@/api/work/stock/stock.js";

	export default {
		data() {
			return {
				nextPageFlag: true,
				query: {
					pageNum: 1,
					pageSize: 10,
					skuName: ''
				},
				show: false,
				stockDetailShow: false,
				stockList: [],
				stockDetail: {},
				upSkuCode: '',
				skuName: '',
				totalCount: 0,
				current: 0,
				swiperCurrent: 0,
				loadStatus: ['loadmore', 'loadmore', 'loadmore', 'loadmore'],
			};
		},
		onLoad() {
			this.getStockList();
		},
		computed: {

		},
		methods: {
			confirm() {
				setTimeout(() => {
					// 3秒后自动关闭
					this.show = false;
					updateSkuCode({
						skuName: this.skuName,
						skuCode: this.upSkuCode
					}).then(response => {
						uni.navigateTo({
							url: `/pages/stock/index`
						});
					})
				}, 1000)
			},
			showModal(item) {
				this.show = true;
				this.skuName = item;
			},
			changeNumBox(e) {
				console.log(e)
			},
			// 触底懒加载
			reachBottom() {
				console.info("触底----开始加在stockList")
				this.loadStatus.splice(this.current, 1, "loading")
				if (this.nextPageFlag) {
					setTimeout(() => {
						console.info("延迟1.1s")
						this.getStockList(this.current);
					}, 1100);
				}

			},
			getImage: function(e) {
				uni.chooseImage({
					success: function(res) {
						let data = {
							name: 'file',
							filePath: res.tempFilePaths[0]
						}
						uploadImg(data).then(response => {
							let dataImg = {
								skuName: e,
								itemImg: response.data.url
							}
							updateItemImg(dataImg).then(response => {
								uni.navigateTo({
									url: `/pages/stock/index`
								});
							})
						})
					},
				})
			},
			updateStock(item) {

				if (item.showNumberBox != true) {
					//设置初始值
					item.oriNum = item.num;
					item.showNumberBox = true;
					item.buttonName = "保存";
				} else {
					if (item.oriNum > item.num) {
						this.$modal.alert("当前仅支持增加库存，请重试");
						item.num = item.oriNum
					} else if (item.oriNum == item.num) {
						//如果相等，则什么都不做
					} else {
						let data = {
							skuName: item.skuName,
							num: item.num - item.oriNum
						}
						addStock(data).then(response => {
							if (response.code == 200) {
								this.$modal.msgSuccess("库存更新成功");
								setTimeout(() => {
									uni.navigateTo({
										url: `/pages/stock/index`
									});
								}, 1000);

							}
						})
					}
					item.showNumberBox = false;
					item.buttonName = "库存更新";
				}
			},
			searchKey(keyword) {
				this.query.pageNum = 1
				this.stockList = []
				this.getStockList()
			},
			getDetail(skuName) {
				this.stockDetailShow = true;
				const query = {'keyword':skuName};
				stockListDetail(query).then(response => {
					if (response.code == 200) {
						this.stockDetail = response.data
					}
				})
				// uni.navigateTo({
				// 	url: `/pages/sales/index?skuName=${skuName}`
				// });
			},
			gotoSales(skuName) {
				uni.redirectTo({
					url: `/pages/sales/index?skuName=${skuName}`
				});
			},
			// 页面数据
			getStockList() {
				this.nextPageFlag = false;
				const pageTotal = this.totalCount / this.query.pageSize
				console.info('pageTotal:' + pageTotal)
				console.info('pageNum:' + this.query.pageNum)
				if (true) {
					setTimeout(() => {
						stockList(this.query).then(response => {
							this.stockList.push(...response.rows)
							this.totalCount = response.total
							//如果 rows.length<pageSize,说明没有下一页了
							if (response.rows.length < this.query.pageSize) {
								this.nextPageFlag = false;
							} else {
								this.query.pageNum++
								this.nextPageFlag = true;
							}
						})
					}, 500)
					this.loadStatus.splice(this.current, 1, "loadmore")
				} else {
					this.loadStatus.splice(this.current, 1, "noMore")
				}
			}
		}
	};
</script>

<style>
	/* #ifndef H5 */
	page {
		height: 100%;
		background-color: #f2f2f2;
	}

	/* #endif */
</style>

<style lang="scss" scoped>
	.minus {
		width: 22px;
		height: 22px;
		border-width: 1px;
		border-color: #2979ff;
		border-top-left-radius: 100px;
		border-top-right-radius: 100px;
		border-bottom-left-radius: 100px;
		border-bottom-right-radius: 100px;
		@include flex;
		justify-content: center;
		align-items: center;
	}

	.input {
		padding: 0 10px;
	}

	.plus {
		width: 22px;
		height: 22px;
		background-color: #2979ff;
		border-radius: 50%;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		justify-content: center;
		align-items: center;
	}

	.order {
		width: 710rpx;
		background-color: #ffffff;
		margin: 20rpx auto;
		border-radius: 20rpx;
		box-sizing: border-box;
		padding: 20rpx;
		font-size: 28rpx;

		.item {
			display: flex;
			flex-direction: row;
			margin: 20rpx 0 0;

			.right {
				margin-top: 100rpx;
				margin-right: 80rpx;

				image {
					width: 200rpx;
					height: 200rpx;
					border-radius: 10rpx;
				}
			}

			.content {
				flex: 1;

				/* 占据剩余空间 */
				.title {
					font-size: 38rpx;
					line-height: 50rpx;
					margin-bottom: 10rpx;
				}

				.type {
					margin: 10rpx 0;
					font-size: 28rpx;
					color: $u-tips-color;
				}

				.delivery-time {
					color: #e5d001;
					font-size: 28rpx;
				}
			}
		}

		.number-box {
			button-size: "5";
			color: "#ffffff";
			bgColor: "#2979ff";
			iconStyle: "color: #fff";
		}

		.total {
			margin-top: 20rpx;
			text-align: right;
			font-size: 24rpx;

			.total-price {
				font-size: 32rpx;
			}
		}

		.bottom {
			display: flex;
			margin-top: 40rpx;
			padding: 0 10rpx;
			justify-content: space-between;
			align-items: center;

			.btn {
				line-height: 52rpx;
				width: 160rpx;
				border-radius: 26rpx;
				border: 2rpx solid $u-border-color;
				font-size: 26rpx;
				text-align: center;
				color: $u-info-dark;
			}

			.evaluate {
				color: $u-warning-dark;
				border-color: $u-warning-dark;
			}
		}
	}

	.centre {
		text-align: center;
		margin: 200rpx auto;
		font-size: 32rpx;

		image {
			width: 164rpx;
			height: 164rpx;
			border-radius: 50%;
			margin-bottom: 20rpx;
		}

		.tips {
			font-size: 24rpx;
			color: #999999;
			margin-top: 20rpx;
		}

		.btn {
			margin: 80rpx auto;
			width: 200rpx;
			border-radius: 32rpx;
			line-height: 64rpx;
			color: #ffffff;
			font-size: 26rpx;
			background: linear-gradient(270deg, rgba(249, 116, 90, 1) 0%, rgba(255, 158, 1, 1) 100%);
		}
	}

	.wrap {
		display: flex;
		flex-direction: column;
		height: calc(100vh - var(--window-top));
		width: 100%;
	}

	.swiper-box {
		flex: 1;
	}

	.swiper-item {
		height: 100%;
	}
</style>